﻿/* File Created: August 29, 2012 */
// Raw Html object
Proggy.CorePropertyTypes.RawHtml = function () {

    // variables
    var area, hidden, textArea;

    // onBlur method
    var doOnBlur = function (ed) {

        // select all
        CodeMirror.commands["selectAll"](ed);

        // get range
        var range = {
            from: ed.getCursor(true),
            to: ed.getCursor(false)
        };

        // format
        ed.autoFormatRange(range.from, range.to);

        //**** NOW ENCODE AND MOVE HTML FROM ONE TO OTHER ****//

        // get value
        var cmValue = ed.getValue();
        //console.log("START\n" + cmValue + "\nEND");

        // get encoded
        var encoded = jQuery("<div/>").text(cmValue).html();

        // set input
        hidden.val(encoded);
    };

    // the editor
    this.Editor = undefined;

    // init
    this.Init = function (uniqueIdOfArea) {

        // get editor area
        area = jQuery("#" + uniqueIdOfArea);

        // get text area
        textArea = area.find("textarea:first");
        hidden = area.find("input:first");

        // get initial value
        var initVal = jQuery("<div/>").html(hidden.val()).text();

        // set initial value
        textArea.html(initVal);

        // get id
        var id = textArea.attr("id");

        // set code mirror
        this.Editor = CodeMirror.fromTextArea(document.getElementById(id), {

            //value: val,
            mode: "text/html",
            tabMode: "indent",
            onBlur: function (ed) {

                //
                doOnBlur(ed);
            },
            autofocus: true
        });
    };
};